package net.maku.system.erp.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.system.erp.convert.SysMeasureUnitConvert;
import net.maku.system.erp.entity.SysMeasureUnitEntity;
import net.maku.system.erp.query.SysMeasureUnitQuery;
import net.maku.system.erp.service.SysMeasureUnitService;
import net.maku.system.erp.vo.SysMeasureUnitVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 计量单位管理表
 *
 * @author 王宝林 baolin.wang@aliyun.com
 * @since 1.0.0 2024-03-19
 */
@RestController
@RequestMapping("erp/unit")
@Tag(name="计量单位管理表")
@AllArgsConstructor
public class SysMeasureUnitController {
    private final SysMeasureUnitService sysMeasureUnitService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public Result<PageResult<SysMeasureUnitVO>> page(@ParameterObject @Valid SysMeasureUnitQuery query){
        PageResult<SysMeasureUnitVO> page = sysMeasureUnitService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<SysMeasureUnitVO> get(@PathVariable("id") Long id){
        SysMeasureUnitEntity entity = sysMeasureUnitService.getById(id);

        return Result.ok(SysMeasureUnitConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    public Result<String> save(@RequestBody SysMeasureUnitVO vo){
        sysMeasureUnitService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    public Result<String> update(@RequestBody @Valid SysMeasureUnitVO vo){
        sysMeasureUnitService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    public Result<String> delete(@RequestBody List<Long> idList){
        sysMeasureUnitService.delete(idList);

        return Result.ok();
    }
}